package batchprocessor;

import java.io.File;

public class BatchProcessor {
    
    public static void main(String[] args) throws ProcessException {
        
        System.out.println("***** Batch parser starting *****");

        File file;
        
        // Check number of arguments
        if (args.length != 2) {

            file = new File(args[0]);
            System.out.println(args[0]);

            // Check to make sure file exists
            if (file.exists()) {
                System.out.println("Batch file exists");
            } else {
                System.out.println("Batch file not found");
                throw new ProcessException("Batch file not found");
            }

        } else {
            System.out.println("Usage : BatchProcessor.jar <batch_file.xml> ");
            return;
        }
        
        // Create Batch Object
        Batch batch = new Batch();
        
        // Create parser Object and parse file
        BatchParser parser = new BatchParser(file);
        
        // Set the working directory
        batch.setWkdir(parser.getWkdir());
        
        // Add all of the commands
        while(parser.getNumCommands() > 0){
            batch.addCommand(parser.getCommand());
        }
        
        // Add all of the pipe commands
        while(parser.getNumPipes() > 0) {
            batch.addPipeCommand(parser.getPipe());
        }
        
        batch.start();

        System.out.println("*** Batch Parser Finished ***");
    }
}